﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HackWinMine
{
    internal class WinMineData
    {
        public const int WMN_FRAME_ROWS = 26;
        public const int WMN_FRAME_COLUMNS = 32;
        public const int WMN_ROWS_MAX = 24;
        public const int WMN_COLUMNS_MAX = 30;

        public const uint WMN_DATA_BUFLEN = 1000;
        const uint WMN_DATA_MINE = 0;
        const int WMN_DATA_LEVEL_OFFSET = 864;
        const int WMN_DATA_MINENUM_OFFSET = 868;
        const int WMN_DATA_ROWS_OFFSET = 872;
        const int WMN_DATA_COLUMNS_OFFSET = 876;

        public enum ENUM_MINE_DATA
        {
            FRAME = 0x10,
            MINE = 0x8F,
            EMPTY = 0x0F,
            NULL = -1,
        }

        public byte[] WmnDataBuf { get; set; }
        public WinMineData() {
            WmnDataBuf = new byte[WMN_DATA_BUFLEN];
        }

        public int Rows
        {
            get
            {
                if(WmnDataBuf== null)
                    return 0;
                return BitConverter.ToInt32(WmnDataBuf, WMN_DATA_ROWS_OFFSET);
            }
        }

        public int Columns
        {
            get
            {
                if (WmnDataBuf == null)
                    return 0;
                return BitConverter.ToInt32(WmnDataBuf, WMN_DATA_COLUMNS_OFFSET);
            }
        }

        public ENUM_MINE_DATA getFrameData(int row,int col) {
            byte dat = WmnDataBuf[row * WMN_FRAME_COLUMNS + col];
            if ((dat & 0x80) == 0x80)
                return ENUM_MINE_DATA.MINE;
            return (ENUM_MINE_DATA)dat;
        }
        public ENUM_MINE_DATA getCellData(int row, int col)
        {
            byte dat = WmnDataBuf[(row + 1) * WMN_FRAME_COLUMNS + (col + 1)];
            if ((dat & 0x80) == 0x80)
                return ENUM_MINE_DATA.MINE;
            return (ENUM_MINE_DATA)dat;
        }
    }
}
